package com.kitten.chatglmsdk.session.defaults;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.kitten.chatglmsdk.executor.Executor;
import com.kitten.chatglmsdk.model.*;
import lombok.extern.slf4j.Slf4j;
import com.kitten.chatglmsdk.IOpenAiApi;
import com.kitten.chatglmsdk.session.Configuration;
import com.kitten.chatglmsdk.session.OpenAiSession;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.sse.EventSource;
import okhttp3.sse.EventSourceListener;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * @description: 会话服务
 */
@Slf4j
public class DefaultOpenAiSession implements OpenAiSession {

    /**
     * OpenAi 接口
     */
    private final Configuration configuration;

    private final Map<Model, Executor> executorGroup;

    public DefaultOpenAiSession(Configuration configuration, Map<Model, Executor> executorGroup) {
        this.configuration = configuration;
        this.executorGroup = executorGroup;
    }


    public EventSource completions(ChatCompletionRequest chatCompletionRequest, EventSourceListener eventSourceListener) throws Exception {
        Executor executor = (Executor)this.executorGroup.get(chatCompletionRequest.getModel());
        if (null == executor) {
            throw new RuntimeException(chatCompletionRequest.getModel() + " 模型执行器尚未实现！");
        } else {
            return executor.completions(chatCompletionRequest, eventSourceListener);
        }
    }

    public CompletableFuture<String> completions(ChatCompletionRequest chatCompletionRequest) throws Exception {
        Executor executor = (Executor)this.executorGroup.get(chatCompletionRequest.getModel());
        if (null == executor) {
            throw new RuntimeException(chatCompletionRequest.getModel() + " 模型执行器尚未实现！");
        } else {
            return executor.completions(chatCompletionRequest);
        }
    }



    public ChatCompletionSyncResponse completionsSync(ChatCompletionRequest chatCompletionRequest) throws Exception {
        log.info("请求体1：{}", JSON.toJSONString(chatCompletionRequest));
        Executor executor = (Executor)this.executorGroup.get(chatCompletionRequest.getModel());
        if (null == executor) {
            throw new RuntimeException(chatCompletionRequest.getModel() + " 模型执行器尚未实现！");
        } else {
            log.info("执行模型实现, 执行器:");
            return executor.completionsSync(chatCompletionRequest);
        }
    }

    /**
     * 图像生成接口
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public ImageCompletionResponse imageGeneration(ImageCompletionRequest request) throws Exception {
        Executor executor = this.executorGroup.get(request.getModelEnum());
        if (executor == null) {
            throw new RuntimeException(request.getModel() + " 模型执行器尚未实现");
        } else {
            return executor.imageGeneration(request);
        }
    }

    public Configuration configuration() {
        return this.configuration;
    }


}
